import * as mobx from 'mobx';
import {observable, action, computed, asMap} from 'mobx';
import {WeaForm} from 'comsMobx';
import * as API from '../apis/communication';
import isFunction from 'lodash/isFunction';
import { message } from 'antd';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

export default class CommunicationStore {

    baseParam = {};
    communicationId = -1;
    @observable hasdate = true;  //是否无数据
    @observable adSearchCondition = [];
    @observable adSearchForm = new WeaForm();
    @observable contentList = [];  //评论列表
    @observable status = {
        tabKey : "list",
        pageSize: 10,
        currentPage: 1,
        total:100
    }
    //是否允许关联流程、文档、上传附件等
    @observable appStatus = {};

    //与我相关小红点
    @observable hasDot = false;
    //相关资源列表
    @observable resource = {
        readed : false,
        docList : [],
        wfList : [],
        cusList : [],
        mutilprjList : [],
        taskList : [],
        accList : [],
    }

    @observable hasSubmitRight = false;
    @action
    init(params={}){
        this.baseParam = params;
        API.getInitParams(this.baseParam).then(data => {
            this.appStatus = data.appStatus;
            this.baseParam.communicationId = data.communicationId;
            this.hasSubmitRight = data.hasSubmitRight;
            this.hasDot = data.hasRelatedMsg;
            this.adSearchCondition = data.adSearchCondition;
            this.adSearchForm.initFormFields(this.adSearchCondition);
            this.reflashContentList();
        })
    }

    getResource = (params) => {
        API.getResource(this.baseParam).then(data => {
            this.resource.readed = true;
            this.resource.docList = data.docList;
            this.resource.wfList = data.wfList;
            this.resource.cusList = data.cusList;
            this.resource.mutilprjList = data.mutilprjList;
            this.resource.taskList = data.taskList;
            this.resource.accList = data.accList;
        })
    }

    /**
     * 刷新评论列表
     */
    reflashContentList = (params) => {
        let adSearchFormParams = {};
        if(this.status.tabKey == "list") {
            adSearchFormParams = this.adSearchForm.getFormParams();
        }
        if(this.baseParam.communicationId > 0) {
            API.getContentList({type : this.status.tabKey,...this.baseParam, pageSize:this.status.pageSize, currentPage: this.status.currentPage, ...adSearchFormParams, ...params}).then(data => {
              this.contentList = data.contentList;
              // this.contentList = this.getReplaceImgSrc(data.contentList);
              this.status.total = data.count;
              this.hasdate = data.hasdate;
                
            })
        }
    }

    @action getReplaceImgSrc = (data) => {
      let result  = [];
      data.length > 0 &&  data.map (v => {
        if(v.quoteVo && v.quoteVo.showRemark) {
          if(v.quoteVo.showRemark.indexOf('<img') > -1 ) {
            let beforeStr = v.quoteVo.showRemark.slice(0,v.quoteVo.showRemark.indexOf('<img'));
            let afterStr = v.quoteVo.showRemark.slice(v.quoteVo.showRemark.indexOf('style'));
            let str = v.quoteVo.showRemark.slice(v.quoteVo.showRemark.indexOf('data-imgsrc'), v.quoteVo.showRemark.indexOf('original-src')-2)
            let strTemp = str.slice(str.indexOf('=')+2);
            v.quoteVo.showRemark = beforeStr + '<img src="' + strTemp +'"  ' +  afterStr;
          }
        }
        result.push(v)
      })
      return result;
    }
    /**
     * 保存评论
     */
    saveContent = (params, callBack) => {
        let str = params.remark;
        if(params.remark != '' && params.remark.indexOf('<img') < 0) {
          str = params.remark.replace(/<\/?[^>]*>/g, '');
          str = str.replace(/[\u200B-\u200D\uFEFF]/g, "");
          str = str.replace(/\n/g,'');
          str = str.replace("&#8203;", "").replace(/&nbsp;/g, '');
          str = str.trim();
        } 
        if (!str) {
          message.destroy();
          message.error(getLabel(382648, '请输入交流内容'));
          return false;
        }
        API.doSaveContent({...this.baseParam,...params}).then(data => {
            if(data.result) {
                this.reflashContentList();
            }
            isFunction(callBack) && callBack();
        })
    }

    /**
     * 编辑交流内容
     */
    editContent = (params, callBack) => {
        API.doEditContent({...this.baseParam,...params}).then(data => {
            if(data.canModify === false){
                message.destroy();
                message.error("已超过规定时间或有新内容，无法编辑！");
            }
            this.reflashContentList();
            isFunction(callBack) && callBack();
        })
    }

    /**
     * 保存回复
     */
    saveReply = (params, callBack) => {
        if(params.replyType == "quote") {//引用
            this.saveContent(params, callBack);
        } else {
            API.doSaveReply({...params, communicationId : this.baseParam.communicationId}).then(data => {
                if(data.result) {
                    this.reflashContentList();
                }
                isFunction(callBack) && callBack();
            })
        }
    }

    operateCommunication = (params) => {
        if(params.operationType == "agree") {
            let newContentList = [];
            API.doApprove(params).then(data => {
                this.contentList.map(item => {
                    if(item.contentId == data.contentId) {
                        item.approveNum = data.approveCount;
                        item.approved = data.hasApprove;
                    }
                    newContentList.push(item);
                })
                this.contentList = newContentList;
            })
        } else {
            API.doDelete({...this.baseParam,...params}).then(data => {
                if(data.canDelete === false) {
                    message.destroy();
                    message.error(getLabel('512478','已超过规定时间或有新内容，无法删除'));
                } else {

                }
                this.reflashContentList();
            })
        }
    }

    @action
    setState = (params = {}, callBack) => {
        Object.keys(params).forEach(key => {
            this.status[key] = params[key];
        });
        isFunction(callBack) && callBack();
        return this;
    }

    @action
    hideDot = () => {
        this.hasDot = false;
    }
}

